// Copyright 2017 Baidu Inc. All Rights Reserved.
// Author: Su Hao (suhao@baidu.com)
//
// Description: Wrapper for mutex

#include "baidu_ca_mutex.h"
#include "baidu_ca_internal.h"
#include "baidu_ca_memory.h"

typedef struct _baidu_ca_mutex_cbs_s {
    bca_mutex_create_f f_create;
    bca_mutex_lock_f f_lock;
    bca_mutex_unlock_f f_unlock;
    bca_mutex_destroy_f f_destroy;
} bca_mutex_cbs;

BCA_LOC_IMPL bca_mutex_cbs s_bca_mutex_cbs = {NULL};

BCA_EXT_IMPL void baidu_ca_mutex_init(bca_mutex_create_f f_create,
                                      bca_mutex_lock_f f_lock,
                                      bca_mutex_unlock_f f_unlock,
                                      bca_mutex_destroy_f f_destroy) {
    s_bca_mutex_cbs.f_create = f_create;
    s_bca_mutex_cbs.f_lock = f_lock;
    s_bca_mutex_cbs.f_unlock = f_unlock;
    s_bca_mutex_cbs.f_destroy = f_destroy;
}

BCA_INT_IMPL bca_mutex_t bca_mutex_create() {
    return s_bca_mutex_cbs.f_create ? s_bca_mutex_cbs.f_create() : NULL;
}

BCA_INT_IMPL bca_status_t bca_mutex_lock(bca_mutex_t mutex) {
    return s_bca_mutex_cbs.f_lock ? s_bca_mutex_cbs.f_lock(mutex) : BCA_ERR_INTERNAL;
}

BCA_INT_IMPL bca_status_t bca_mutex_unlock(bca_mutex_t mutex) {
    return s_bca_mutex_cbs.f_unlock ? s_bca_mutex_cbs.f_unlock(mutex) : BCA_ERR_INTERNAL;
}

BCA_INT_IMPL bca_status_t bca_mutex_destroy(bca_mutex_t mutex) {
    return s_bca_mutex_cbs.f_destroy ? s_bca_mutex_cbs.f_destroy(mutex) : BCA_ERR_INTERNAL;
}
